package com.meepo.framework.design.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;

import com.meepo.framework.R;
import com.meepo.framework.design.helper.Utils;

public class SlickButton extends AppCompatButton {

    public static final int BUTTON_TYPE_DEFAULT = 0;
    public static final int BUTTON_TYPE_PRIMARY = 1;

    private int mCornerRadius;
    private int mColor;
    private int mType;

    private final Context mContext;

    public SlickButton(@NonNull Context context) {
        this(context, null);
    }

    public SlickButton(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, android.R.attr.buttonStyle);
    }

    public SlickButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = context;

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SlickButton, defStyleAttr, 0);

        mCornerRadius = typedArray.getDimensionPixelSize(R.styleable.SlickButton_btn_cornerRadius, 0);
        mColor = typedArray.getColor(R.styleable.SlickButton_btn_color, context.getResources().getColor(R.color.primary_text_color));
        mType = typedArray.getInt(R.styleable.SlickButton_btn_type, BUTTON_TYPE_DEFAULT);

        typedArray.recycle();

        this.setPadding(15, 0, 15, 0);

        this.setAllCaps(false);

        this.setEllipsize(TextUtils.TruncateAt.END);

        this.setMaxLines(1);

        notifyDataChanged();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if (mCornerRadius == 0) {
            mCornerRadius = this.getHeight() / 2;
            this.setCornerRadius(mCornerRadius);
        }

    }

    private void notifyDataChanged() {
        notifyBackgroundChanged();
    }

    private void notifyBackgroundChanged() {
        GradientDrawable bg = new GradientDrawable();
        bg.setCornerRadius(mCornerRadius);

        switch (mType) {
            case BUTTON_TYPE_PRIMARY:
                bg.setColor(mColor);
                break;
            default:
                bg.setColor(Color.TRANSPARENT);
                bg.setStroke(Utils.dp2px(mContext, 1), mColor);
                this.setTextColor(mColor);
        }

        this.setBackground(bg);

    }

    public int getCornerRadius() {
        return mCornerRadius;
    }

    public void setCornerRadius(int cornerRadius) {
        this.mCornerRadius = cornerRadius;
        notifyDataChanged();
    }


    public int getColor() {
        return mColor;
    }

    public void setColor(int mColor) {
        this.mColor = mColor;
        notifyDataChanged();
    }


    public int getType() {
        return mType;
    }

    public void setType(int mType) {
        this.mType = mType;
        notifyBackgroundChanged();
    }
}
